package org.dut.community.controller;

import org.dut.community.entity.DiscussPost;
import org.dut.community.entity.Page;
import org.dut.community.entity.User;
import org.dut.community.service.DiscussPostService;
import org.dut.community.service.LikeService;
import org.dut.community.service.UserService;
import org.dut.community.util.CommunityConstant;
import org.dut.community.util.HostHolder;
import org.dut.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController implements CommunityConstant {
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private UserService userService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HostHolder hostHolder;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String getIndexPage(Model model, Page page,
                               @RequestParam(name = "orderMode", defaultValue = "1") int orderMode) {
        // 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model.
        User user = hostHolder.getUser();
        if (user != null) {
            boolean hasUnreadPosts = discussPostService.hasUnreadPosts(user.getId());
            model.addAttribute("hasUnreadPosts", hasUnreadPosts);
        }

        List<DiscussPost> list = new ArrayList<>();
        if (orderMode == 2) {
            if (user == null) {
                return "/site/login";
            }
            int cnt = discussPostService.findFolloweePostCount(user.getId());
            page.setRows(cnt);
            list = discussPostService.selectFolloweePosts(user.getId(), page.getOffset(), page.getLimit());
            // 清除帖子未读标记
            discussPostService.markPostsAsRead(user.getId());
        } else {
            page.setRows(discussPostService.selectDiscussPostRows(0));
            list = discussPostService.selectDiscussPosts(0, page.getOffset(), page.getLimit(), orderMode);
        }
        page.setPath("/index?orderMode=" + orderMode);

        List<Map<String, Object>> listMap = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (DiscussPost post : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                user = userService.findUserById(post.getUserId());
                map.put("user", user);
                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);
                map.put("postReadCount", discussPostService.getPostReadCount(post.getId()));

                listMap.add(map);
            }
        }
        model.addAttribute("list", listMap); // 与 ThymeLeaf对应
        model.addAttribute("orderMode", orderMode);
        return "/index";
    }

    @RequestMapping(value = "/denied", method = RequestMethod.GET)
    public String getDeniedPage() {
        return "/site/error/404";
    }

    // 重定向到错误页面
    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
        return "/site/error/500";
    }
}
